import 'package:flutter/material.dart';
import './save/sp_util.dart';
import 'package:get/get.dart';

/// @class : LocaleOptions
/// @date : 2024/12/31
/// @name : shrek
/// @description :语言工具类
class Language {
  String name = '';
  String language = '';
  String country = '';
  bool isSelect = false;

  Language(this.name, this.language, this.country, {this.isSelect = false});

  Language.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        language = json['language'],
        country = json['country'],
        isSelect = json['isSelect'];

  Map<String, dynamic> toJson() => {
        'name': name,
        'language': language,
        'country': country,
        'isSelect': isSelect,
      };
}

class LocaleOptions {
  ///更新语言
  static updateLocale(Language language) {
    Locale? locale;
    // print(2222222);
    // print(language.language);
    // print(language.country);

    if (language.language == '' || language.country == '') {
      locale = Get.deviceLocale;
    } else {
      locale = Locale(language.language, language.country);
    }
    if (locale != null) {
      Get.updateLocale(locale);
    }
  }

  ///获取当前存储的默认语言
  static getDefault() {
    var language = SpUtil.getLanguage();
    if (language == null || language.language == '' || language.country == '') {
      return Get.deviceLocale;
    } else {
      return Locale(language.language, language.country);
    }
  }
}
